package search.users;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import server.Initialize;

import ajaxmethods.SessionMethod;

public class SearchSPs extends SessionMethod {

	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		super.perform(request, response);
		request.getSession().setAttribute("currobjectid",null);
		GetServiceProviders getSPs = new GetServiceProviders();
		StringBuffer agentsBuffer = getSPs.getSPs("");
		StringBuffer sb = new StringBuffer();
		sb.append("<div id=\"homeWorkspaceWrapper\"><div id=\"agentWorkspace\">");
		sb.append("<div id=\"shortSections\">");
		
		sb.append("<div style=\"text-align:left;\"><div style=\"width:440px;float:left;\">");
		sb.append("<h3 style=\"margin-bottom:5px;text-align:left;\">Up And Coming Service Provider - Feb. 2011</h3><div class=\"g-section g-tpl-25-75 content\">"+
          "<div class=\"g-unit\" >"+
          "<div>Bob ServiceProvider</div>" +
          "<div>Bob's Plumbing Office</div>" +
          "<div>Bob's Ratings</div> " +
          "<a>Email Bob For a Quote</a>." +
          "</div>" +
          "<div class=\"g-unit g-first\"> <img src=\""+Initialize.appURL()+"/images/othernopicture.jpg\" width=\"100\" height=\"75\"> </div>"+
          "</div></div>");
		sb.append("<div style=\"text-align:left;float:right;\"><div style=\"width:440px;\">");
		sb.append("<h3 style=\"margin-bottom:5px;text-align:left;\">Top Producing Service Provider - Feb. 2011</h3><div class=\"g-section g-tpl-25-75 content\">"+
          "<div class=\"g-unit\" >"+
          "<div>Jim Title</div>" +
          "<div>Jim Title's Title Office</div>" +
          "<div>Jim Title Ratings</div> " +
          "<a>Email Jim Title</a>." +
          "</div>" +
          "<div class=\"g-unit g-first\"> <img src=\""+Initialize.appURL()+"/images/nopicture.gif\" width=\"100\" height=\"75\"> </div>"+
          "</div></div></div></div></div>");
		
		sb.append("<div id=\"head\"><p class=\"textbox\" style=\"position:relative;text-align:left;float:left;margin: 20px 0px 0px 10px;\"><label class=\"absolutelbl whitelbl\">Find Service Provider</label>" +
		"<input type=\"text\" id=\"searchprovider\" class=\"esoinput\" onkeyup=\"findSPs(this)\" style=\"display:inline;width:180px;\" name=\"email\" title=\"searchagent\">");
		sb.append("<img style=\"position:absolute;padding-left:2px;cursor:pointer;top:4px;height:22px;right:0px;\" src=\""+Initialize.appURL()+"/images/search-icon.jpg\"></p>");
		sb.append("<div style=\"position:relative;width:160px;\" id=\"action\"><a style=\"float:left;\" class=\"prev esobutton\" >&#x25B2;</a>" +
		"<span id=\"noobjectsfound\">No Agents Found</span><span id=\"displaynumobjects\" style=\"width:120px;position:absolute;padding:0px 1px;\"><span id=\"start\">1</span>&nbsp;to&nbsp;<span id=\"end\">4</span>&nbsp;of&nbsp;<span id=\"total\">&nbsp;"+getSPs.getNumberSPs()+"&nbsp;</span></span>" +
		"<a style=\"float:right;\" class=\"next esobutton\" >&#x25BC;</a></div></div>");

		sb.append("<script>$(document).ready(function(){$('#agentWorkspace .verticalscroll').scrollable({vertical: true,touch:false, mousewheel: true," +
				"onSeek:function(){" +
				"$('#action #start').html(parseInt(4*this.getIndex())+1);" +
				"$('#action #end').html(Math.min(parseInt(4*this.getSize()),parseInt(4*this.getIndex())+4));" +
				" }" +
				"});});</script>");
		sb.append("<div class=\"scrollable\"" +
				"><div id=\"noscrollsidepanel\"><p>Finding a Service Provider is easy.  Type the name of the service you are seeking (ie, insurance, plumbing, electrical...) into the textfield above and voil&aacute;!</p></div>" +
				"<div class=\"verticalscroll\"><div class=\"items\">");
		
		sb.append(agentsBuffer);
		sb.append("</div></div></div>");
		sb.append(this.javaScriptOut());
		try {
			data.put("HOME", sb.toString()).write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private StringBuffer javaScriptOut() {
		StringBuffer sb = new StringBuffer();
		sb.append("<script>");
		sb.append("function findSPs(elem) {\n" +
				"	doPost('search.users.GetServiceProviders',{searchserviceprovider:elem.value});\n" +
				"}\n");
		sb.append("</script>");
		return sb;	
	}
}
